using System;
using System.Collections.Generic;
using UnityEngine;

public class MoveListDemo : MonoBehaviour
{
	[Serializable]
	public class MoveSegment
	{
		public KineticAnimationsStateEnum animation;

		[Range(-1f, 1f)]
		public float xAdjust;

		[Range(-1f, 1f)]
		public float zAdjust;
	}

	public List<MoveSegment> moveList = new List<MoveSegment>();

	[Header("Params")]
	public Transform startPositionTransform;

	public Transform endPositionTransform;

	[Header("Display")]
	public int numStepsToFind;

	[Range(0f, 1f)]
	public float crossFadeDuration;

	[Range(0.05f, 10f)]
	public float timeScale;

	public bool relativeCoordinates;

	public bool goLeftRight;

	public bool useDesiredEnd;

	[Header("Output")]
	public Vector3 positionError;

	public Vector3 endPosition;

	public Quaternion endRotation;

	public KineticPlan kineticPlan;

	[Header("Keyboard controls")]
	public Vector3 offset;

	public List<KineticAnimationsStateEnum> leftKeyAnims;

	public List<KineticAnimationsStateEnum> rightKeyAnims;

	public HitAnimationsStateEnum hitEnum;

	[Range(0.5f, 2f)]
	public float animatorSpeed = 1f;

	public float offsetSpeed = 1f;

	private int hOffsetIndex;

	public void Update()
	{
		Time.timeScale = timeScale;
		positionError = endPosition - base.transform.position;
		if (Input.GetMouseButtonDown(0) && kineticPlan.indexOfElementOfType<KineticElementHit>() != 0)
		{
			kineticPlan.CancelAllPendingElements();
			hOffsetIndex = 0;
			KineticElementHit kineticElementHit = new KineticElementHit();
			kineticElementHit.animInfo = ScriptableObjectSingleton<KineticAnimationInfoDB>.instance.GetOrCreateHitAnimationByEnum(hitEnum);
			kineticElementHit.crossFadeDuration = crossFadeDuration;
			kineticElementHit.startFromCurrentPosition = true;
			kineticElementHit.adjustHandToHitPosition = false;
			kineticPlan.AddElement(kineticElementHit);
			return;
		}
		if (Input.GetAxisRaw("Horizontal") == 0f)
		{
			offset = Vector3.zero;
			hOffsetIndex = 0;
			return;
		}
		offset = new Vector3(Input.GetAxisRaw("Horizontal"), 0f, Input.GetAxis("Vertical"));
		KineticPlanElement activeElement = kineticPlan.activeElement;
		if ((kineticPlan.activeElement == null || (activeElement.ellapsedTime + activeElement.animatorSpeed * Time.deltaTime >= activeElement.length && kineticPlan.Count == 1)) && kineticPlan.indexOfElementOfType<KineticElementHit>() < 1)
		{
			kineticPlan.CancelAllPendingElements();
			if (Mathf.Sign(offset.x) != Mathf.Sign(hOffsetIndex))
			{
				hOffsetIndex = (int)Mathf.Sign(offset.x);
			}
			else
			{
				hOffsetIndex += (int)Mathf.Sign(hOffsetIndex);
			}
			List<KineticAnimationsStateEnum> list = ((hOffsetIndex < 0) ? leftKeyAnims : rightKeyAnims);
			MoveAnimationInfo moveAnimationInfo = ScriptableObjectSingleton<KineticAnimationInfoDB>.instance.GetAnimation(list[Mathf.Clamp(Mathf.Abs(hOffsetIndex), 0, list.Count) - 1].ToString()) as MoveAnimationInfo;
			Vector3 vector = Vector3.forward * (moveAnimationInfo.zAdjustRange.LerpMinMax(offset.z) - moveAnimationInfo.positionChange.z);
			KineticElementMove kineticElementMove = new KineticElementMove();
			kineticElementMove.animInfo = moveAnimationInfo;
			kineticElementMove.animatorSpeed = animatorSpeed;
			kineticElementMove.crossFadeDuration = crossFadeDuration;
			kineticElementMove.useDesiredEnd = true;
			kineticElementMove.startFromCurrentPosition = true;
			kineticElementMove.desiredEndPosition = base.transform.position + base.transform.rotation * (moveAnimationInfo.positionChange + vector);
			kineticPlan.AddElement(kineticElementMove);
		}
	}

	public void Play()
	{
		bool flag = true;
		Vector3 zero = Vector3.zero;
		Quaternion identity = Quaternion.identity;
		Vector3 position = startPositionTransform.position;
		Quaternion rotation = startPositionTransform.rotation;
		base.transform.position = position;
		base.transform.rotation = rotation;
		foreach (MoveSegment move in moveList)
		{
			Quaternion quaternion = rotation * identity;
			Vector3 startPosition = position + zero;
			KineticElementMove kineticElementMove = new KineticElementMove();
			if (flag)
			{
				flag = !relativeCoordinates;
				kineticElementMove.startPosition = startPosition;
				kineticElementMove.startRotation = quaternion;
			}
			else
			{
				kineticElementMove.startFromCurrentPosition = true;
			}
			kineticElementMove.crossFadeDuration = crossFadeDuration;
			MoveAnimationInfo moveAnimationInfo = ScriptableObjectSingleton<KineticAnimationInfoDB>.instance.GetAnimation(move.animation.ToString()) as MoveAnimationInfo;
			Vector3 zero2 = Vector3.zero;
			zero2 = ((!goLeftRight) ? (Vector3.right * moveAnimationInfo.xAdjustRange.LerpMinMax(move.xAdjust) + Vector3.forward * moveAnimationInfo.zAdjustRange.LerpMinMax(move.zAdjust)) : new Vector3(0f, 0f, 0f - moveAnimationInfo.positionChange.z));
			kineticElementMove.animInfo = moveAnimationInfo;
			kineticElementMove.useDesiredEnd = useDesiredEnd;
			zero += quaternion * (moveAnimationInfo.positionChange + zero2);
			identity *= moveAnimationInfo.rotationChange * Quaternion.AngleAxis(kineticElementMove.adjustAngle, Vector3.up);
			kineticElementMove.desiredEndPosition = position + zero;
			kineticPlan.AddElement(kineticElementMove);
		}
		endPosition = position + zero;
		endRotation = identity * rotation;
	}

	public void GoToPosition()
	{
		Vector3 position = startPositionTransform.position;
		Vector3 vector = endPositionTransform.position.OnGround();
		KineticPlan.AnimSearchSolution animSearchSolution = kineticPlan.FindPathToGetToPosition(new KineticPlan.SearchParams
		{
			startPos = startPositionTransform.position,
			startRot = startPositionTransform.rotation,
			endPos = endPositionTransform.position.OnGround(),
			maxDepth = 5
		});
		numStepsToFind = animSearchSolution.steps;
		moveList.Clear();
		goLeftRight = false;
		foreach (MoveAnimationInfo anim in animSearchSolution.animList)
		{
			MoveSegment moveSegment = new MoveSegment();
			moveSegment.animation = ScriptableObjectSingleton<KineticAnimationInfoDB>.instance.GetEnumForAnimation(anim);
			moveSegment.xAdjust = animSearchSolution.xAdjust;
			moveSegment.zAdjust = animSearchSolution.zAdjust;
			moveList.Add(moveSegment);
		}
	}
}
